moddbfandomcom-20200213-history
DirectX:DirectInput:Tutorials:VB:DX7:Keyboard Handling
DirectInput is the component of DirectX that gives you direct access to the current state of the keyboard (among other devices). This is obviously quite useful for game programming purposes and so I will outline its use here. Dim dx As New DirectX7 Dim di As DirectInput Dim diDEV As DirectInputDevice Set di = dx.DirectInputCreate() Set diDEV = di.CreateDevice("GUID_SysKeyboard") diDEV.SetCommonDataFormat DIFORMAT_KEYBOARD diDEV.SetCooperativeLevel Me.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE diDEV.Acquire As with DirectDraw, we have to create an instance of the DirectX7 object and use it to define a DirectInput object, di. We then need to set the direct input device to the one in which we are interested, in this case the keyboard. So, we call the di.CreateDevice method and request it returns the system keyboard object. Next we have to set the data format of the device to the keyboard data format. We do this with the diDEV.SetCommonDataFormat. We could also use this to set the data format to Joy1, Joy2, or the mouse. Just like DirectDraw, we must set the cooperative level. We pass the handle to the window for which we would like to receive keyboard input, if this is the form in which the code resides we can pass Me.hWnd. DISCL_BACKGROUND and DISCL_NONEXCLUSIVE allows other programs access to keystrokes, and give us access to all keystrokes. Now that we've set up the device which we'd like to use, we have to acquire it using the diDEV.Acquire method. Dim diState As DIKEYBOARDSTATE Dim i As Integer Public aKeys(211) As Boolean diDEV.GetDeviceStateKeyboard diState For i = 1 To 211 If diState.Key(i) <> 0 Then aKeys(i) = True Else aKeys(i) = False End If Next Everything is setup, now we can reap what we have sown (no, I'm not suggesting you go plant your keyboard in your garden in order to cultivate input). diState is an object that will contain the states of all the keys on the keyboard, and we can tap into this using an array and a loop. Each key on the keyboard corresponds to an integer value between 0 and 211. If we then make an array with 212 members we can keep track of which keys are active and when. Use the diDEV.GetDeviceStateKeyboard method to fill the diState variable, and then extract the data from this into our array aKeys using a for loop. Click here for sample source code demonstrating DirectInput keyboard handling in action. This may be useful: Private Function KeyName(Key As Byte) As String Select Case Key Case DIK_ESCAPE KeyName = "ESCAPE" Case DIK_1 KeyName = "1" Case DIK_2 KeyName = "2" Case DIK_3 KeyName = "3" Case DIK_4 KeyName = "4" Case DIK_5 KeyName = "5" Case DIK_6 KeyName = "6" Case DIK_7 KeyName = "7" Case DIK_8 KeyName = "8" Case DIK_9 KeyName = "9" Case DIK_0 KeyName = "0" Case DIK_MINUS KeyName = "-" Case DIK_EQUALS KeyName = "=" Case DIK_BACK KeyName = "BACKSPACE" Case DIK_TAB KeyName = "TAB" Case DIK_Q KeyName = "Q" Case DIK_W KeyName = "W" Case DIK_E KeyName = "E" Case DIK_R KeyName = "R" Case DIK_T KeyName = "T" Case DIK_Y KeyName = "Y" Case DIK_U KeyName = "U" Case DIK_I KeyName = "I" Case DIK_O KeyName = "O" Case DIK_P KeyName = "P" Case DIK_LBRACKET KeyName = "Case DIK_RBRACKET KeyName = "" Case DIK_RETURN KeyName = "RETURN" Case DIK_LCONTROL KeyName = "LEFT CONTROL" Case DIK_A KeyName = "A" Case DIK_S KeyName = "S" Case DIK_D KeyName = "D" Case DIK_F KeyName = "F" Case DIK_G KeyName = "G" Case DIK_H KeyName = "H" Case DIK_J KeyName = "J" Case DIK_K KeyName = "K" Case DIK_L KeyName = "L" Case DIK_SEMICOLON KeyName = ";" Case DIK_APOSTROPHE KeyName = "'" Case DIK_GRAVE KeyName = "`" Case DIK_LSHIFT KeyName = "LEFT SHIFT" Case DIK_BACKSLASH KeyName = "\" Case DIK_Z KeyName = "Z" Case DIK_X KeyName = "X" Case DIK_C KeyName = "C" Case DIK_V KeyName = "V" Case DIK_B KeyName = "B" Case DIK_N KeyName = "N" Case DIK_M KeyName = "M" Case DIK_COMMA KeyName = "," Case DIK_PERIOD KeyName = "." Case DIK_SLASH KeyName = "/" Case DIK_RSHIFT KeyName = "RIGHT SHIFT" Case DIK_MULTIPLY KeyName = "NUMPAD MULTIPLY" Case DIK_LMENU KeyName = "LEFT ALT" Case DIK_SPACE KeyName = "SPACE" Case DIK_CAPITAL KeyName = "CAPS LOCK" Case DIK_F1 KeyName = "F1" Case DIK_F2 KeyName = "F2" Case DIK_F3 KeyName = "F3" Case DIK_F4 KeyName = "F4" Case DIK_F5 KeyName = "F5" Case DIK_F6 KeyName = "F6" Case DIK_F7 KeyName = "F7" Case DIK_F8 KeyName = "F8" Case DIK_F9 KeyName = "F9" Case DIK_F10 KeyName = "F10" Case DIK_NUMLOCK KeyName = "NUM LOCK" Case DIK_SCROLL KeyName = "SCROLL LOCK" Case DIK_NUMPAD7 KeyName = "NUMPAD 7" Case DIK_NUMPAD8 KeyName = "NUMPAD 8" Case DIK_NUMPAD9 KeyName = "NUMPAD 9" Case DIK_SUBTRACT KeyName = "NUMPAD SUBTRACT" Case DIK_NUMPAD4 KeyName = "NUMPAD 4" Case DIK_NUMPAD5 KeyName = "NUMPAD 5" Case DIK_NUMPAD6 KeyName = "NUMPAD 6" Case DIK_ADD KeyName = "NUMPAD ADD" Case DIK_NUMPAD1 KeyName = "NUMPAD 1" Case DIK_NUMPAD2 KeyName = "NUMPAD 2" Case DIK_NUMPAD3 KeyName = "NUMPAD 3" Case DIK_NUMPAD0 KeyName = "NUMPAD 0" Case DIK_DECIMAL KeyName = "NUMPAD DECIMAL" Case DIK_F11 KeyName = "F11" Case DIK_F12 KeyName = "F12" Case DIK_F13 KeyName = "F13" Case DIK_F14 KeyName = "F14" Case DIK_F15 KeyName = "F15" Case DIK_NUMPADENTER KeyName = "NUMPAD ENTER" Case DIK_RCONTROL KeyName = "RIGHT CONTROL" Case DIK_NUMPADCOMMA KeyName = "NUMPAD ," Case DIK_DIVIDE KeyName = "/" Case DIK_SYSRQ KeyName = "PRINT SCREEN" Case DIK_RMENU KeyName = "RIGHT ALT" Case DIK_HOME KeyName = "HOME" Case DIK_UP KeyName = "UP" Case DIK_PRIOR KeyName = "PAGE UP" Case DIK_LEFT KeyName = "LEFT" Case DIK_RIGHT KeyName = "RIGHT" Case DIK_END KeyName = "END" Case DIK_DOWN KeyName = "DOWN" Case DIK_NEXT KeyName = "PAGE DOWN" Case DIK_INSERT KeyName = "INSERT" Case DIK_DELETE KeyName = "DELETE" Case DIK_LWIN KeyName = "LEFT WINDOWS" Case DIK_RWIN KeyName = "RIGHT WINDOWS" Case DIK_APPS KeyName = "APPLICATION" Case DIK_PAUSE KeyName = "PAUSE" End Select End Function Category:VB Category:Tutorial Category:DirectX Category:DirectInput